package com.congee02;

import java.lang.ref.SoftReference;

/**
 * @author congee(congee02 @ 163.com)
 * @date 1/28/2023 6:15 PM
 */
public class SoftReferenceTest {

    // VM Options :  -Xmx10M
    // 软引用多用于缓存
    public static void main(String[] args) {
        // 内存不足时，软引用会被 JVM 回收 ；内存充足时，软引用不会被 JVM 回收 
        SoftReference<SoftSampleObject> reference = getSoftReference();
        int[] nums = new int[3 * 1024 * 1024];
        System.gc();
        System.out.println("gc done");
    }
    
    private static SoftReference<SoftSampleObject> getSoftReference() {
        SoftSampleObject o = new SoftSampleObject();
        return new SoftReference<>(o);
    }

    private static class SoftSampleObject {
        @Override
        protected void finalize() throws Throwable {
            System.out.println("SoftSampleObject: doing finalize");
        }
    }
    
}
